package com.uniledger.exchange.bank;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uniledger.commonlib.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BankCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public int flag;

    private List<BankCard> bankCards;
    private Context context;
    private LayoutInflater inflater;

    public static final int FLAG_CARD = 0;
    public static final int FLAG_BOTTOM = 1;

    private int[] icons = new int[]{R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4,
            R.drawable.f5, R.drawable.f6, R.drawable.f7, R.drawable.f8};

    public BankCardAdapter(Context context) {
        this.context = context;
        this.bankCards = new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
        this.flag = FLAG_CARD;
    }

    public BankCardAdapter(Context context, int flag) {
        this(context);
        this.flag = FLAG_BOTTOM;
    }


    public void addData(List<BankCard> list) {
        //TODO 优化
        bankCards.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(BankCard card) {
        //TODO 调整顺序
        bankCards.add(card);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_bank, parent, false);
        return new CardViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {


        if (viewHolder instanceof CardViewHolder) {
            CardViewHolder cardViewHolder = (CardViewHolder) viewHolder;
            final View bg = cardViewHolder.parent;
            Resources res = context.getResources();
            Palette.from(BitmapFactory.decodeResource(res, icons[position])).generate(new Palette.PaletteAsyncListener() {
                @Override
                public void onGenerated(Palette palette) {
                    Palette.Swatch lightVibrantSwatch = palette.getDarkVibrantSwatch();
                    if (lightVibrantSwatch == null) {
                        for (Palette.Swatch swatch : palette.getSwatches()) {
                            if (null != swatch) {
                                lightVibrantSwatch = swatch;
                                break;
                            }
                        }
                    }
                    GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                            new int[]{colorLight(lightVibrantSwatch.getRgb()), colorBurn(lightVibrantSwatch.getRgb())});
                    bg.setBackgroundDrawable(drawable);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return bankCards.size();
    }

    private int colorBurn(int rgb) {
        //加深颜色
        //int all= (int) (rgb*1.1f);
        //红色
        int red = rgb >> 16 & 0xFF;
        int green = rgb >> 8 & 0xFF;
        int blue = rgb & 0xFF;

        red = (int) Math.floor(red * (1 - 0.2));
        green = (int) Math.floor(green * (1 - 0.2));
        blue = (int) Math.floor(blue * (1 - 0.2));
        return Color.rgb(red, green, blue);

    }


    private int colorLight(int rgb) {
        //颜色变浅
        //int all= (int) (rgb*1.1f);
        //红色
        int red = rgb >> 16 & 0xFF;
        int green = rgb >> 8 & 0xFF;
        int blue = rgb & 0xFF;

        red = (int) Math.floor(red * (1 + 0.2));
        green = (int) Math.floor(green * (1 + 0.2));
        blue = (int) Math.floor(blue * (1 + 0.2));
        return Color.rgb(red, green, blue);

    }


    static class CardViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.bg)
        View parent;

        @BindView(R.id.img)
        ImageView icon;

        @BindView(R.id.bank_name)
        TextView bankName;

        @BindView(R.id.bank_type)
        TextView bankType;

        @BindView(R.id.bank_number)
        TextView bankNumber;

        public CardViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


    }
}
